// Author: wsfuyibing <682805@qq.com>
// Date: 2025-05-09

package models

import (
	"gitee.com/go-libs/db-xorm/db"
	"server/app/config"
	"server/app/errs"
	"server/app/kind"
	"time"
)

// +---------------------------------------------------------------------------+
// | Arcade                                                                    |
// +---------------------------------------------------------------------------+

func (o *ArcadeMember) Available() bool {
	return o.AvailableEnd.Time().Unix() >= time.Now().Unix()
}

// +---------------------------------------------------------------------------+
// | Coupon                                                                    |
// +---------------------------------------------------------------------------+

func (o *Coupon) Available() bool {
	t := time.Now().Unix()
	return t >= o.AvailableBegin.Time().Unix() && t <= o.AvailableEnd.Time().Unix()
}

// +---------------------------------------------------------------------------+
// | Goods                                                                     |
// +---------------------------------------------------------------------------+

func (o *Goods) TotalSales() kind.GoodsStock {
	return o.SaleCount + o.LockedCount
}

// +---------------------------------------------------------------------------+
// | Order                                                                     |
// +---------------------------------------------------------------------------+

// CanClose
// 订单是否可以撤销.
func (o *Order) CanClose() bool {
	return o.State.IsNot() || o.State.IsFailed()
}

func (o *OrderItem) ToGoodsId() kind.GoodsId { return kind.GoodsId(o.ResourceId) }

// CanPay
// 订单是否可以支付.
//
//  1. 订单未支付或支付失败
//  2. 自下单至今未超过600(以实际配置为准)秒
func (o *Order) CanPay() bool {
	if o.State.IsNot() || o.State.IsFailed() {
		return (db.Now().Time().Unix() - o.CreatedAt.Time().Unix()) < config.Runtime.GetOrderLifetime()
	}
	return false
}

func (o *Order) CanPayTime() time.Time {
	return o.CreatedAt.Time().Add(time.Second * time.Duration(config.Runtime.GetOrderLifetime()))
}

func (o *Order) CanRefund() bool {
	return o.State.IsSucceed()
}

// +---------------------------------------------------------------------------+
// | Ticket                                                                    |
// +---------------------------------------------------------------------------+

func (o *TicketSession) Remaining() kind.GoodsStock { return 0 }

// TotalSales
// 合计售出.
func (o *TicketSession) TotalSales() kind.GoodsStock {
	return kind.GoodsStock(o.LockedCount + o.OrderCount)
}

// +---------------------------------------------------------------------------+
// | Qrcode                                                                    |
// +---------------------------------------------------------------------------+

// CanUse
// 校验二维码是否能继续使用.
//
//  1. 未过期
//  2. 未使用过
//  3. 使用次数未到上限
func (o *Qrcode) CanUse() (yes bool, err error) {
	// 1. 过期检查.
	if o.Timeout.IsEnabled() && o.Timeout.IsExpiredFor(o.AppliedAt.Time()) {
		err = errs.ErrQrcodeTimeout
		return
	}

	// 2. 限制次数.
	if o.ScannedLimit.IsEnabled() && o.ScannedLimit.IsReached(o.ScannedCount) {
		err = errs.ErrQrcodeScannedLimit
		return
	}

	// 3. 可以使用.
	yes = true
	return
}
